﻿#include<iostream>
#include<assert.h>
using namespace std;

//template<class T,size_t N=10>
//class stack
//{
//private:
//	int _a[N];
//	size_t _size;
//};
//
//int main()
//{
//	stack<int> s1;
//	stack<int, 5> s2;
//	stack<int, 20> s3;
//
//	return  0;
//}
//


namespace zsy
{
	template<class T, size_t N = 10>
	class array
	{
	public:
		T& operator[](size_t index)
		{
			assert(index < N);
			return _array[index];
		}

		const T& operator[](size_t index) const
		{
			assert(index < N);
			return _array[index];
		}

		size_t size() const
		{
			return _size;
		}

		bool empty() const
		{
			return _size == 0;
		}

	private:
		T _array[N];
		size_t _size;
	};

	void test1()
	{
		array<int> a1;
		array<int, 20> a2;

		//动态数组array和静态数组的区别: 越界检查
		//静态数组是抽查
		//越界读不检查，越界写要检查
		int a3[10];
		cout << a3[11] << endl;
		//a3[11] = 10;//err

		//array越界读写都会检查
		a1[10];
		//a1[11] = 10;//err


	}
}

//int main()
//{
//	zsy::test1();
//
//	return 0;
//}



class Date
{
	friend ostream& operator<<(ostream& _cout, const Date& d);
public:
	Date(int year = 1900, int month = 1, int day = 1)
		: _year(year)
		, _month(month)
		, _day(day)
	{}

	bool operator<(const Date& d)const
	{
		return (_year < d._year) ||
			(_year == d._year && _month < d._month) ||
			(_year == d._year && _month == d._month && _day < d._day);
	}

	bool operator>(const Date& d)const
	{
		return (_year > d._year) ||
			(_year == d._year && _month > d._month) ||
			(_year == d._year && _month == d._month && _day > d._day);
	}
private:
	int _year;
	int _month;
	int _day;
};

ostream& operator<<(ostream& _cout, const Date& d)
{
	_cout << d._year << "-" << d._month << "-" << d._day;
	return _cout;
}

class DateLess
{
public:
	bool operator()(Date* p1, Date* p2)
	{
		return *p1 < *p2;
	}
};



//函数模版
template<class T>
bool Less(const T& x, const T& y)
{
	return x < y;
}

//对Less函数模板特化
template<>
bool Less<Date*>(Date* const& x, Date* const& y)
{
	return *x < *y;
}

template<>
bool Less<const Date*>(const Date* const& x, const Date* const& y)
{
	return *x < *y;
}

bool Less(Date* x, Date* y)
{
	return *x < *y;
}

bool Less(const Date* x, const Date* y)
{
	return *x < *y;
}



int main()
{
	cout << Less(1, 2) << endl;
	
	Date d1(2022, 7, 7);
	Date d2(2022, 7, 8);
	cout << Less(d1, d2) << endl;

	Date* p1 = &d1;
	Date* p2 = &d2;
	cout << Less(p1, p2) << endl;//调用特化之后的版本，而不走模板生成了

	const Date* p3 = &d1;
	const Date* p4 = &d2;
	cout << Less(p3, p4) << endl;

	return 0;
}


template<class T1,class T2>
class Data
{
public:
	Data() { cout << "Data<T1,T2>" << endl; }

private:
	T1 _d1;
	T2 _d2;
};

//全特化
template<>
class Data<int, char>
{
public:
	Data()
	{
		cout << "Data<int,char>" << endl;
	}
private:
	int _d1;
	char _d2;
};

//偏特化/半特化
template<class T1>
class Data<T1, double>
{
public:
	Data()
	{
		cout << "Data<T1,double>" << endl;
	}
private:
	T1 _d1;
	double _d2;
};

template<class T1>
class Data<T1, char>
{
public:
	Data()
	{
		cout << "Data<T1,char>" << endl;
	}
private:
	T1 _d1;
	char _d2;
};



//偏特化 传的类型是指针
template <typename T1,typename T2>
class Data<T1*, T2*>
{
 public:
	 Data() { cout << "Data<T1*,T2*>" << endl; }

private:
	T1 _d1;
	T2 _d2;
};


template <typename T1,typename T2>
class Data<T1&, T2&>
{
public:
	Data(const T1& d1, const T2& d2)
		:_d1(d1)
		,_d2(d2)
	{ 
		cout << "Data<T1&,T2&>" << endl;
	}

private:
	const T1& _d1;
	const T2& _d2;
};

template<class T1,class T2>
class Data<T1&, T2*>
{
public:
	Data()
	{
		cout << "Data<T1&,T2*>" << endl;

		int a = 0;
		T1& x = a;
		T2* y = &a;
		T1 z = a;

		cout << typeid(x).name() << endl;
		cout << typeid(y).name() << endl;
	}

	void Push(const T1& x)
	{}
};

//int main()
//{
//	Data<int, int> d1;
//	Data<int, char> d2;
//	Data<int, double> d3;
//	Data<char, double> d4;
//	Data<char, char> d5;
//	Data<char*, char*> d6;
//	Data<int*, char*> d7;
//	Data<int&, char&> d8;
//	Data<int&, double&> d9;
//	Data<int&, int*> d10;
//
//	return 0;
//}


#include<vector>
#include<algorithm>
template<class T>
struct Less
{
	bool operator()(const T& x, const T& y) const
	{
		return x < y;
	}
};

int main()
{
	Date d1(2022, 7, 7);
	Date d2(2022, 7, 6);
	Date d3(2022, 7, 8);

	//可以直接排序，结果是日期升序
	vector<Date> v1;
	v1.push_back(d1);
	v1.push_back(d2);
	v1.push_back(d3);
	sort(v1.begin(), v1.end(), Less<Date>());

    //这里排序错误，因为走的是Less模板，
	//sort在排序时实际比较的是v2中指针的地址，
	vector<Date*> v2;
	v2.push_back(&d1);
	v2.push_back(&d2);
	v2.push_back(&d3);
	sort(v2.begin(), v2.end(), Less<Date*>());

	return 0;
}